﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApiV3.Data.Entity;
using WebApiV3.Domain;
using WebApiV3.Domain.Enom;
using WebApiV3.InterFace.Log;

namespace WebApiV3.Implement.Log
{
    public class Log : ILog
    {
        private readonly IRespository<Logs> _logRespository;

        public Log(IRespository<Logs> logRespository)
        {
            _logRespository = logRespository;
        }

        public void Clear()
        {
            var list = _logRespository.Table.ToList();
            _logRespository.Delete(list);
        }

        public void Delete(Logs log)
        {
            _logRespository.Delete(log); 
        }

        public Logs GetLogId(int id)
        {
            return _logRespository.GetById(id);
        }

        public void Insert(Logs log)
        {
            _logRespository.Insert(log);
        }

        public IEnumerable<Logs> MultiQuery(DateTime beginTime, DateTime endTime, LogLevel logLevel=LogLevel.错误, int userId = 0)
        {
            var bTime = beginTime == null ? DateTime.Now : beginTime;
            var eTime = endTime == null ? DateTime.Now : endTime;

            var list = _logRespository.Table.Where(x=>x.CreateTime>=bTime && x.CreateTime <=endTime && x.LogLevel == (int)logLevel);

            if (userId !=0)
            {
                list = list.Where(x => x.UserId == userId);
            }
            return list;
        }
    }
}
